package com.murfrosoft.game;

import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;

import com.murfrosoft.gameengine.SingleTouchGameEngine;

public class AirHockeyGame extends SingleTouchGameEngine {


	/* Declare screen size variables ??? (can we auto-detect this?) */
	int HEIGHT = 850;
	int WIDTH = 540;
	int puck_radius = 40;
	int paddle_radius = 60;
	float PUCK_FRICTION = (float) 0.99;
	float puckx = (float) WIDTH/2;
	float pucky = (float) HEIGHT/2;
	float puckx_vel = -10;
	float pucky_vel = -10;
	
	float compx = 100;
	float compy = 100;
	float compx_vel = 2;
	float compy_vel = 2;
	
	float youx = 100;
	float youy = 700;
	float youx_old = 100;
	float youy_old = 700;
	float youx_vel = 0;
	float youy_vel = 0;;
	
	
	public AirHockeyGame(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		
	}

	@Override
	public void updatePhysics() {
		/* UPDATE PUCK POSITION */
		puckx = puckx + puckx_vel;
		pucky = pucky + pucky_vel;
		
		if(puckx + puckx_vel <= 0 + puck_radius || puckx + puckx_vel >= WIDTH - puck_radius){
			puckx_vel = puckx_vel * -1;
		}
		if(pucky + pucky_vel <= 0 + puck_radius || pucky + pucky_vel >= HEIGHT - puck_radius){
			pucky_vel = pucky_vel * -1;
		}
		
		/* UPDATE COMPUTER PADDLE POSITION */
		compx = compx + compx_vel;
		compy = compy + compy_vel;
		if( compx < 90 || compx > 410 ){
			compx_vel *= -1;
		}
		if( compy < 90 || compy > 280 ){
			compy_vel *= -1;
		}
		
		
		/* Add Puck Friction */
		puckx_vel *= PUCK_FRICTION;
		pucky_vel *= PUCK_FRICTION;
	
		/* Control your paddle */
		youx_old = youx;
		youy_old = youy;
		youx = touch.x;
		youy = touch.y;
		// see what kind of velocity this reports (needed for puck collision later)
		youx_vel = youx - youx_old;
		youy_vel = youy - youy_old;
		if( youx_vel > 20 ){
			youx_vel = 20;
		}
		else if( youx_vel < -20 ){
			youx_vel = -20;
		}
		if( youy_vel > 20 ){
			youy_vel = 20;
		}
		else if( youy_vel < -20 ){
			youy_vel = -20;
		}
		/* Keep your puck on YOUR side of table!! */
		if( youy < HEIGHT/2 ){
			youy = HEIGHT/2;
		}
		
		/* CHECK FOR COLLISIONS WITH OUR PADDLE */
		float r_sum = puck_radius + paddle_radius;
		float a = (youx - puckx)*(youx - puckx);
		float b = (youy - pucky)*(youy - pucky);
		if( Math.sqrt(a+b) <= r_sum ){
			float coll_pt_x = (youx + puckx)/2;
			float coll_pt_y = (youy + pucky)/2;
			// calculate new puck velocity:
			// if collision is from below:
			if( coll_pt_x < puckx ){
				puckx_vel = Math.abs(puckx_vel) + youx_vel;
			}
			else{
				puckx_vel = (-1)*Math.abs(puckx_vel) - youx_vel;  // have no idea if this really works
			}
			if( coll_pt_y < pucky ){
				pucky_vel = Math.abs(pucky_vel) + youy_vel;
			}
			else{
				pucky_vel = (-1)*Math.abs(pucky_vel) - youy_vel;  // have no idea if this really works
			}
		}
		
		/* Check for collision with computer paddle */
		a = (compx - puckx)*(compx - puckx);
		b = (compy - pucky)*(compy - pucky);
		if( Math.sqrt(a+b) <= r_sum ){
			float coll_pt_x = (compx + puckx)/2;
			float coll_pt_y = (compy + pucky)/2;
			// calculate new puck velocity:
			// if collision is from below:
			if( coll_pt_x < puckx ){
				puckx_vel = Math.abs(puckx_vel) + compx_vel;				
			}
			else{
				puckx_vel = (-1)*Math.abs(puckx_vel) - compx_vel;  // have no idea if this really works
			}
			if( coll_pt_y < pucky ){
				pucky_vel = Math.abs(pucky_vel) + compy_vel;
			}
			else{
				pucky_vel = (-1)*Math.abs(pucky_vel) - compy_vel;  // have no idea if this really works
			}
		}
		
		/* Force puck in-bounds */
		if( puckx < puck_radius ){
			puckx = puck_radius;
		}
		else if( puckx > WIDTH - puck_radius ){
			puckx = WIDTH - puck_radius;
		}
		if( pucky < puck_radius ){
			pucky = puck_radius;
		}
		else if( pucky > HEIGHT - puck_radius ){
			pucky = HEIGHT - puck_radius;
		}
		
	}
	

	@Override
	public void draw(Canvas canvas) {
    
		/* NOTES:  James, I don't know where to place globals, or new classes, 
		 *   		in this code.  I'm doing a practice run using non-objects in the
		 *          draw method.  This is messy and inefficient, but should be playable
		 *          as a mock-up.  I'll need your help to do this properly!
		 */
		
		/* DRAW SOME SORT OF BOARD */
	    canvas.drawColor(Color.rgb(200,200,200));
	    Paint line_color = new Paint();
	    line_color.setColor(Color.rgb(0,180,0));
	    canvas.drawRect(0, HEIGHT/2+4, WIDTH,  HEIGHT/2-4, line_color);
	    // Draw goals
	    canvas.drawRect(WIDTH/2 - 130, 0, WIDTH/2 + 130, 40, line_color);
	    canvas.drawRect(WIDTH/2 - 130, HEIGHT-40, WIDTH/2 + 130, HEIGHT, line_color);
	    
	    /* Debug Text */
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		canvas.drawText("X:" + touch.x +" Y:" + touch.y, 20, 20, paint);
		canvas.drawText("Xv:" + youx_vel + " Yv:" + youy_vel, 20,40, paint);
		
		Paint paddle_color1 = new Paint();
		paddle_color1.setColor(Color.rgb(255, 100, 100));
		Paint paddle_color2 = new Paint();
		paddle_color2.setColor(Color.rgb(255, 0, 0));
		Paint paddle_color3 = new Paint();
		paddle_color3.setColor(Color.rgb(0, 0, 255));
		Paint paddle_color4 = new Paint();
		paddle_color4.setColor(Color.rgb(100, 100, 255));
		
		/* DRAW PUCK */
		canvas.drawCircle(puckx, pucky, puck_radius, paint);
		if( Math.sqrt( (puckx - youx)*(puckx-youx) + (pucky - youy)*(pucky-youy)) <= (puck_radius + paddle_radius)){
			canvas.drawCircle(puckx, pucky, puck_radius, paddle_color1);
		}
		
		/* DRAW TEST PADDLE */
		canvas.drawCircle(youx, youy, paddle_radius, paddle_color2);
		canvas.drawCircle(youx, youy, paddle_radius/2, paddle_color1);
		
		/* DRAW COMPUTER PADDLE */
		canvas.drawCircle(compx,compy, paddle_radius, paddle_color3);
		canvas.drawCircle(compx,compy, paddle_radius/2, paddle_color4);
	}

}
